from kimmo import *
import feature

class KimmoInterface(object):
	def __init__(self, lexiconFile, ruleFile):
		kc = KimmoControl(None, None, lexiconFile, ruleFile, False)
		if not kc.ok:
			raise kc.errors

		self._kimmoSet = kc.s
		self._morphology = kc.m
	
	def recognize(self, word):
		all = self._kimmoSet.recognize(word, self._morphology)
		result = []
		for rec in all:
			features = [word.gloss() for word in rec[1]]
			categoryParts = map(lambda f: feature.Category.parse(f), features)
			category = reduce(lambda a, b: a.mix(b), categoryParts, feature.Category.null())
			result.append((rec[0], category))
		return result
			

	def generate(self, tokens):
		gen = self._kimmoSet.generate(tokens)
		return [x for x in gen]
	
# rec = kc.s.recognize("coger", kc.m)

